﻿using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.SessionState;

namespace PSSsoft.Web
{
    public partial class VerifyCode1 : System.Web.UI.Page
    {
        #region Page_Load 输出一个验证码图片
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/plain";

            //特别注意，如不加，单击验证图片＇看不清换一张＇，无效果． 
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //调用方法，产生一个完整的图片验证码
            this.CreateCheckCodeImage(GenerateCheckCode());
        }
        #endregion

        #region 产生一个验证码的字符串

        /// <summary>
        /// 产生一个验证码的字符串
        /// </summary>
        /// <returns></returns>
        private string GenerateCheckCode()
        {
            System.Random random = new System.Random();
            string checkCode = random.Next(1000, 9999).ToString();
            //将验证码保存到Cookies
            //注意Cookies的key值，在其他页面会使用这个key值
            Response.Cookies.Add(new HttpCookie("ValidateCode", checkCode));
            return checkCode;
        }
        #endregion

        #region 产生一个图片验证码,并且在内存中输出
        /// <summary>
        /// 产生一个图片验证码
        /// </summary>
        /// <param name="checkCode">验证码字符串</param>
        private void CreateCheckCodeImage(string checkCode)
        {
            if (string.IsNullOrEmpty(checkCode))
            {
                return;
            }
            using (Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    //生成随机生成器 
                    Random random = new Random();
                    //清空图片背景色 
                    g.Clear(Color.White);
                    //画图片的背景噪音线 
                    for (int i = 0; i < 4; i++)
                    {
                        int x1 = random.Next(image.Width);
                        int x2 = random.Next(image.Width);
                        int y1 = random.Next(image.Height);
                        int y2 = random.Next(image.Height);
                        g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                    }
                    //图片的字体
                    Font font = new Font("Arial", 12, (FontStyle.Bold));
                    Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
                    LinearGradientBrush brush = new LinearGradientBrush(rectangle, Color.Blue, Color.DarkRed, 1.2f, true);
                    //画图片的字符串（就是验证码字符）
                    g.DrawString(checkCode, font, brush, 2, 2);
                    //画图片的前景噪音点 
                    for (int i = 0; i < 12; i++)
                    {
                        int x = random.Next(image.Width);
                        int y = random.Next(image.Height);
                        image.SetPixel(x, y, Color.FromArgb(random.Next()));
                    }
                    //画图片的边框线 
                    g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                    //将图片输出
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    image.Save(ms, ImageFormat.Gif);
                    Response.ClearContent();
                    Response.ContentType = "image/Gif";
                    Response.BinaryWrite(ms.ToArray());
                }
            }

        }
        #endregion

    }
}